/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const validMap = ["admin", "editor"];
  return validMap.indexOf(str.trim()) >= 0;
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return reg.test(url);
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function looselyValidURL(url) {
  const reg = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;
  return reg.test(url);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
//   const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  const reg = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return reg.test(email);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === "string" || str instanceof String) {
    return true;
  }
  return false;
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]";
  }
  return Array.isArray(arg);
}

/**
 * 必填验证未通过时定位到该元素位置
 */
export function scrollToElement(className = "has-error", options) {
  const dom = document.querySelector(`.${className}`);
  dom?.scrollIntoView(options || { block: "center" });
}

function getLengthLE(length, required = true, otherRuleArray = [], trigger = "change", message = "") {
  return [
    ...otherRuleArray,
    {
      required,
      validator(rule, value, callback) {
        if (!value) {
          return rule.required ? callback(new Error(`请输入${message}`)) : callback();
        } if (value.length <= length) {
          return callback();
        }
        return callback(new Error(`内容字数已超【${value.length}/${length}】`));
      },
      trigger
    }
  ];
}

// 必填且字数少于20个
export const arrayRequired = [{ type: "array", required: true, message: "必填", trigger: ["blur", "change"] }];
export const simpleRequired = [{ required: true, message: "必填", trigger: ["blur", "change"] }];

// 可自定义提示消息的验证
export const requiredLengthLE20 = (msg = "") => getLengthLE(20, undefined, undefined, undefined, msg);
export const customMsgRequired = (msg = "", type = "input", completeMsg, other = {}) => {
  const message = completeMsg || `请${type === "input" ? "输入" : "选择"}${msg}`;
  return [{ required: true, message, trigger: ["blur", "change"], ...other }];
};